<template>
    <div>
        <el-form :inline="true" :model="params" class="demo-form-inline">
            <el-form-item label="批次号">
                <el-input v-model="params.batchNum" placeholder="批次号"></el-input>
            </el-form-item>
            <el-form-item label="激活码类型">
                <el-select v-model="params.type" placeholder="分类">
                    <el-option :label="item.value" :value="item.name" v-for="(item,index) in codeType" :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="激活状态">
                <el-select v-model="params.state" placeholder="激活状态">
                    <el-option value="" label="请选择" ></el-option>
                    <el-option value="true" label="是" ></el-option>
                    <el-option value="false" label="否" ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="fetch">查询</el-button>
                <el-button type="primary" @click="exportsList">批量导出列表</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="sourceData" style="width: 100%" class='pch-table' stripe>
            <el-table-column prop="id" width="100" label="ID"></el-table-column>
             <el-table-column prop="batchNum"  label="生成批次号"></el-table-column>
            <el-table-column prop="state"  label="激活状态">
                <template slot-scope="scope">
                    <span>{{!scope.row.state?'未激活':'已激活'}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="code"  label="激活码"></el-table-column>
            <el-table-column prop="type" label="发布时间">
                <template slot-scope="scope">
                    <span>{{renderType(scope.row.type)}}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="params.pageNumber"
            :page-size="params.pageSize"
            background
            layout="prev, pager, next, jumper"
            :total="params.total">
        </el-pagination>
    </div>
</template>

<script>
    import api from '@/api'
    import Exp from '@/utils/validateFrom'
    import {codeType} from '@/config'
    console.log(codeType)
    export default {
        data() {
            return {
                dialogVisible: false,
                sourceData: [],
                codeType:codeType,
                params: {
                    batchNum:'',
                    type: '',
                    state: '',
                    pageNumber: 1,
                    pageSize: 10,
                }
            }
        },
        created(){
            this.fetch()
        },
        methods: {
            renderType(val){
                let result = ''
                this.codeType.map(item=>{
                    if(item.name==val){
                        return result =item.value
                    }
                })
                return result
            },
            handleCurrentChange(val) {
                this.params.pageNumber = val
                this.fetch()
            },
            renderDate(uninx) {
                uninx*=1;
                return uninx.toDate()
            },
            exportsList(){
                api.postActiveCodeExport(this.params).then(res=>{
                    if (res.code == 200 ) {
                        let {data} = res;
                        window.open(data);
                    }
                })
            },
            del(row){
                this.$confirm('此操作会删除该新闻, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    api.newsDel({id: row.id}).then((res) => {
                        if (res.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.dialogVisible = false
                            this.fetch()
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            fetch(){
                // let data = [
                //   {
                //     "batchNum": 20180809164040,
                //     "code": "string",
                //     "id": 0,
                //     "state": false,
                //     "type": "employer"
                //   }
                // ];

                // this.sourceData = data
                // return false
                api.getActiveCodeList(this.params).then((res) => {
                    if (res.code == 200 && res.data) {
                        let {data} = res;
                        this.sourceData = data.content
                        this.params.total = data.totalElements
                    }
                })
            },
        },
        components: {}
    }
</script>


<style lang="scss">
</style>

